package com.homedo.microservice.distributed.id.service.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.exception.HomedoException;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.distributed.id.service.bean.po.LeafAlloc;
import com.homedo.microservice.distributed.id.service.bean.po.enhanced.LeafAllocEnhanced;
import com.homedo.microservice.distributed.id.service.persistence.dao.LeafAllocDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 作者 jgshun
 * @date 创建时间 2019年03月07日 15时09分30秒
 */
@Service
public class LeafAllocService extends BaseService<LeafAlloc> {
    @Autowired
    private LeafAllocDao leafAllocDao;
    @Autowired
    private SegmentService segmentService;


    @Override
    public List<LeafAllocEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<LeafAlloc> leafAllocs = leafAllocDao.getListByIds(ids, LeafAlloc.class);

        if (!Detect.notEmpty(leafAllocs)) {
            return null;
        }

        return this.convent2Enhanceds(leafAllocs);
    }


    @Override
    public List<LeafAllocEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<LeafAlloc> leafAllocs = leafAllocDao.getListByIdsGreaterThanMark0(ids, LeafAlloc.class);

        if (!Detect.notEmpty(leafAllocs)) {
            return null;
        }

        return this.convent2Enhanceds(leafAllocs);
    }


    @Override
    public List<LeafAllocEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<LeafAlloc> leafAllocs = (List<LeafAlloc>) pos;
        List<LeafAllocEnhanced> leafAllocEnhanceds = Lists.newArrayList();
        leafAllocs.forEach(e -> leafAllocEnhanceds.add(new LeafAllocEnhanced(e)));
        return leafAllocEnhanceds;
    }

    public boolean checkExist(String key) {
        return segmentService.getIdGen().getCache().keySet().contains(key);
    }

    public void save(String key, UserContext userContext) {
        if (!this.checkExist(key)) {
            synchronized (this) {
                if (!this.checkExist(key)) {
                    LeafAlloc leafAllocRecord = new LeafAlloc();
                    leafAllocRecord.setBizTag(key);

                    LeafAlloc leafAlloc = leafAllocDao.getOneByRecord(leafAllocRecord);
                    if (leafAlloc == null) {
                        if (key.length() > 100) {
                            throw new HomedoException("[key不能超过100个字符:" + key + "]");
                        }
                        leafAlloc = new LeafAlloc();
                        leafAlloc.setBizTag(key);
                        leafAlloc.setMaxId(1L);
                        leafAlloc.setStep(2000);
                        leafAlloc.setDescription("[自动创建]");
                        leafAlloc.setUpdateTime(new Date());

                        leafAllocDao.save(leafAlloc);
                    }

//                    segmentService.getIdGen().updateCacheFromDb();
                }

            }
        }
    }

}
